package com.jk.controller;

import com.jk.po.HotelPO;
import com.jk.po.TravelAreaPO;
import com.jk.service.HomePageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @ClassName: HomePageController
 * @Description:TODO
 * @Auther: 王宇超
 * @Date: 2021/6/30 20:22
 * @version : V1.0
 */
@RestController
@RequestMapping("homePage")
@Api("主页")
public class HomePageController {
    private static Logger logger = LoggerFactory.getLogger(HomePageController.class);
    @Autowired
    private HomePageService homePageService;

    /**
    * @Author  王宇超
    * @method  queryHappyPlay
    * @description 查看欢乐游玩
    * @Date 20:47 2021/6/30
    * @Param  * @param
    * @return java.util.List<com.jk.po.TravelAreaPO>
    **/
    @GetMapping("queryHappyPlay")
    @ApiOperation("查看欢乐游玩")
    public void queryHappyPlay() throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"spots"+"/";



        List<TravelAreaPO> list = homePageService.queryHappyPlay();
        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;
        try{
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("spots.ftl");
                fileWriter = new FileWriter(filePath + "spots" + ".html");
                Map<String,List<TravelAreaPO>> map = new HashMap<String, List<TravelAreaPO>>();
                map.put("spots",list);
                template.process(map,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
    * @Author 景晨璇
    * @method  TravelAreaPO
    * @description 描述一下方法的作用
    * @Date 14:11 2021/7/5
    * @Param  * @param id
    * @return void
    **/
    @PostMapping("TravelAreaPO/{id}")
    @ApiOperation(value = "回显景区信息",produces ="id")
    public void TravelAreaPO(@PathVariable("id")@ApiParam("景区id")Integer id) throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"zoo"+"/";

        TravelAreaPO travelAreaPO = homePageService.queryHomeById(id);
        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;
        try{
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("zoo.ftl");
                fileWriter = new FileWriter(filePath + "index" + ".html");

                template.process(travelAreaPO,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @PostMapping("queryYuYueById/{id}")
    @ApiOperation(value = "回显景区预定信息",produces ="id")
    public void queryYuYueById(@PathVariable("id")@ApiParam("景区id")Integer id) throws IOException {
        //获取路径
        File file = new File("");
        String path =file.getCanonicalPath();
        //字符串拼接路径
        String filePath=path+"/"+"lvyou-consumer"+"/"+"src/"+"main/"+"resources/"+"static/"+"nav"+"/"+"yuding"+"/";

        TravelAreaPO travelAreaPO = homePageService.queryYuYueById(id);
        FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigurationFactoryBean.setTemplateLoaderPath("classpath:templates");
        Configuration configuration = null;
        FileWriter fileWriter = null;
        try{
            try {
                configuration = freeMarkerConfigurationFactoryBean.createConfiguration();
                Template template = configuration.getTemplate("yuding.ftl");
                fileWriter = new FileWriter(filePath + "index" + ".html");

                template.process(travelAreaPO,fileWriter);
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if ( fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
